package com.cloud.cmt.usermanage.controller;

import com.angela.cloud.common.base.ResultMessage;
import com.angela.cloud.common.constant.ResultCodeConstant;
import com.cloud.cmt.usermanage.entity.UserRoleRelation;
import com.cloud.cmt.usermanage.po.TenantIdByUplinkPo;
import com.cloud.cmt.usermanage.service.UserRoleRelationRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/11/27.
 */
@RestController
@RequestMapping("/user_role/")
@Api(value = "/用户角色关联", description = "用户角色关联接口-by wcy")
public class UserRoleRelationCtl {
    @Resource
    UserRoleRelationRepository userRoleRelationRepository;

    @ApiOperation(value = "通过id获取信息")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public UserRoleRelation getUser(@PathVariable long id){
        return (UserRoleRelation) userRoleRelationRepository.findOne(id);
    }

    @ApiOperation(value = "保存信息")
    @PostMapping(value = "")
    @ResponseBody
    public ResultMessage<UserRoleRelation> save(@RequestBody UserRoleRelation userRoleRelation){
        ResultMessage resultMessage = new ResultMessage();
        userRoleRelation = userRoleRelationRepository.save(userRoleRelation);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        resultMessage.setId(userRoleRelation.getId()+"");

        return resultMessage;
    }

    @ApiOperation(value = "更新信息")
    @PutMapping(value = "")
    @ResponseBody
    public ResultMessage<UserRoleRelation> update(@RequestBody UserRoleRelation RoleRightRelation){
        ResultMessage resultMessage = new ResultMessage();
        userRoleRelationRepository.saveAndFlush(RoleRightRelation);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);

        return resultMessage;
    }

    @ApiOperation(value = "树查询，根据角色查询权限id，和租户id获取子节点，顶级节点为-1")
    @PutMapping(value = "/findByTenantIdAndRoleId")
    @ResponseBody
    public List<UserRoleRelation> findByTenantIdAndRoleId(@RequestBody TenantIdByUplinkPo po){
        return userRoleRelationRepository.findByTenantIdAndUserId(po.getTenantId(),po.getUplink());
    }

    @ApiOperation(value = "通过id删除")
    @DeleteMapping(value = "/{id}")
    public ResultMessage remove(@PathVariable("id") Long id) {
        ResultMessage resultMessage = new ResultMessage();
        userRoleRelationRepository.delete(id);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        return  resultMessage;
    }
}
